package com.jxpanda.infrastructure.core.tollkit;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

/**
 * @author Panda
 */
public class ObjectKit {

    /**
     * 返回对象是否为空
     * 满足以下条件的对象视为空
     * 1：引用为null
     * 2：Map类型的话，里面没有元素
     * 3：List类型，里面没有元素
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object object) {
        boolean isEmpty = object == null;

        if (isEmpty) {
            return true;
        }
        if (object instanceof String) {
            isEmpty = ((String) object).isEmpty();
        } else if (object instanceof Map) {
            isEmpty = ((Map) object).isEmpty();
        } else if (object instanceof Collection) {
            isEmpty = ((Collection) object).isEmpty();
        }

        return isEmpty;
    }

    public static boolean isNotEmpty(Object object) {
        return !isEmpty(object);
    }

    /**
     * 对象列表是否全都为空
     */
    public static boolean isAllEmpty(Object... objects) {
        return Arrays.stream(objects).allMatch(ObjectKit::isEmpty);
    }

}
